package com.imy.luascript.app.ui.precode;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.ahmadaghazadeh.editor.widget.CodeEditor;
import com.imy.common.base.adapter.BaseDelegate;
import com.imy.common.helper.ParamsHelper;
import com.imy.common.util.FileUtils;
import com.imy.common.util.ToastUtil;
import com.imy.lua.LuaCore;
import com.imy.luascript.R;
import com.imy.luascript.base.activity.BaseViewActivity;
import com.imy.luascript.viewmodel.BaseViewModel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PreViewCodeActivity extends BaseViewActivity {
    @BindView(R.id.editor)
    CodeEditor editor;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.save_btn)
    LinearLayout saveBtn;
    @BindView(R.id.run_btn)
    LinearLayout runBtn;
    PreViewCodeAdapter preViewCodeAdapter ;
    public static void start(Context context, String path, String title) {
        Intent intent = new Intent(context, PreViewCodeActivity.class);
        intent.putExtra(ParamsHelper.PATH, path);
        intent.putExtra(ParamsHelper.TITLE, title);
        context.startActivity(intent);
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected void initView() {

        initAllFile();
    }

    private void initAllFile() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        getFileList();
         preViewCodeAdapter = new PreViewCodeAdapter(self, getFileList());
        String filePath = getFilePath();
        File file = new File(filePath);
        preViewCodeAdapter.getCodeFileDelegate().setCurrentFile(file);
        String stringForFile = FileUtils.getStringForFile(file);
        Log.i(TAG, "string:" + stringForFile);
        editor.setText(stringForFile, 1);
        setToolbarTitle(file.getName());
        recyclerView.setAdapter(preViewCodeAdapter);
        preViewCodeAdapter.hideLoadMore();
        preViewCodeAdapter.getCodeFileDelegate().setOnItemClickListener(new BaseDelegate.ItemClickListener<File>() {
            @Override
            public void onItemClicked(int position, File o, View view) {
                String stringForFile1 = FileUtils.getStringForFile(o);
                editor.setText(stringForFile1, 1);
                preViewCodeAdapter.getCodeFileDelegate().setCurrentFile(o);
                preViewCodeAdapter.notifyDataSetChanged();
                setToolbarTitle(o.getName());
            }
        });
    }


    private List<File> getFileList() {
        List<File> list = new ArrayList<>();
        File file = new File(getFilePath());
        File parentFile = file.getParentFile();
        File[] files = parentFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file1 = files[i];
            if (file1.getAbsolutePath().endsWith(".lua")
                    || file1.getAbsolutePath().endsWith(".properties"))
                list.add(files[i]);
        }
        return list;
    }

    private String getFilePath() {
        return getIntent().getStringExtra(ParamsHelper.PATH);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_code;
    }




    @OnClick(R.id.save_btn)
    public void onSaveBtnClicked() {
        ToastUtil.toast(self,"暂未实现");
//        String text = editor.getText();
//        File currentFile = preViewCodeAdapter.getCodeFileDelegate().getCurrentFile();
//        boolean b = FileUtils.saveStringToFile(text, currentFile.getAbsolutePath());
//        if (b){
//            ToastUtil.toast(self,"保存成功");
//        }
    }

    @OnClick(R.id.run_btn)
    public void onRunBtnClicked() {
        File file = new File(getFilePath());
        File parentFile = file.getParentFile();
        //锁定mainlua
        String s = parentFile.getAbsolutePath() + "/main.lua";
        File file1 = new File(s);
        if (!file1.exists()){
            ToastUtil.toast(self,"main.lua文件不存在，请创建主入口文件");
            return;
        }
        LuaCore.get().start(self,s);
    }
}
